<?php
/**
 * Created by PhpStorm OF yuyi.
 * User: YUYI
 * Date: 2018/5/14 0014
 * Time: 下午 2:24
 * Administrator
 */

namespace app\common\model;



use app\common\service\UserService;
use think\Model;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

class Menus extends BaseModel
{
    protected $pk = 'id';

    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'createTime';
    protected $updateTime = 'updateTime';

    protected $auto = ['createUserId'];

    protected $append = ['typeText','flagText'];

    public function getActiveAttr($value,$data)
    {
        return substr($data['url'],1)==request()->path()?true:false;
    }

    public function setCreateUserIdAttr($value,$data)
    {
        return UserService::getCurrentUserInfo()['userId'];
    }

    /**
     * 获取类型文本
     * @param $value
     * @param $data
     * @return string
     */
    public function getTypeTextAttr($value,$data)
    {
        switch ($data['type']){
            case '1':
                return '管理菜单';
                break;
            case '2':
                return '前端菜单';
                break;
            default:return '未知类型';
        }
    }

    /**获取状态文本
     * @param $value
     * @param $data
     * @return string
     */
    public function getFlagTextAttr($value,$data)
    {
        switch ($data['flag']){
            case '10':
                return '不显示';
            case '20':
                return '显示但置灰';
            case '30':
                return '显示并标新';
            case '99':
                return '正常';
            default:return '未知状态';
        }
    }

    /**
     * 获取子菜单
     * @return $this|Model|BelongsTo
     */
    public function getSonMenu()
    {
        return $this->belongsTo('Menus','pid','id')->joinType('left')->setEagerlyType(0);
    }

    /**
     * 获取父菜单
     * @return $this|Model|BelongsTo
     */
    public function getParentMenu()
    {
        return $this->belongsTo('Menus','id','pid')->joinType('left')->setEagerlyType(0);
    }

    /**
     * 获取创建者信息
     * @return $this|Model|HasOne
     */
    public function getCreateUser()
    {
        return $this->hasOne('Users','userId','createUserId')->joinType('left')->setEagerlyType(0);
    }
}